import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// import TableControls from '#cpt/temp/TableControls'
import Table from '#cpt/temp/Table'
import Input from '@antd/form/input'
// ===================================================================== global declare
const { $fn, $Lazy,$http } = window
// ===================================================================== Lazy
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))

const width = '33%'
let clear
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		//修改账期
		formData:[
			{ label: '账期',			name: 'summary_sn', type: 'select',  loading:true, required:true, width, disabled:true,
				onValue:({value,formData})=>{
					const {hosp_id} = this.formRef.getValue(['hosp_id'])
					if(value&&hosp_id)this.getBill({hosp_id,summary_sn:value})
				} ,
			},
			{ label: '受检单位',		name: 'hosp_id', 	type: 'select',  loading:true, nameStr:'name', required:true, width,disabled:true,
				onValue:({value,formData})=>{
					const {summary_sn} = this.formRef.getValue(['summary_sn'])
					if(value&&summary_sn)this.getBill({summary_sn,hosp_id:value})
				}
			 },
			{ label: '开票单位',		name: 'organization_name',  width,disabled:true },
			{ label: '账单金额',		name: 'bill_price', 	width, required:true, disabled:true },
			{ label: '开票金额',		name: 'invoice_price', 	width, required:true, type:'number', outer:1,disabled:true,
				onValue:({value,formData})=>{
					const { not_invoice_price,bill_price } = this.formRef.getValue()
					if(value){
						value = value.toFixed(2)
					}
					clearTimeout(clear)
					clear = setTimeout(()=>{
						this.formRef.setValue({
							wait_invoice_price : bill_price - not_invoice_price - value,
							invoice_price: value,
						})
					})
					this.setState({formData})
				}
			},
			{ label: '不开票金额',	name: 'not_invoice_price', 	width, required:true, type:'number', outer:1,value:0,disabled:true,
				onValue:({value,formData})=>{
					
					if(value){
						value = value.toFixed(2)
					}
					clearTimeout(clear)
					clear = setTimeout(()=>{
						this.formRef.setValue({ not_invoice_price: value })
					})
					this.setState({formData})
				}
			 },
			{ label: '未开票金额',	name: 'wait_invoice_price', width, required:true, disabled:true },
			{ label: '业务员',		name: 'salesman_id', 		type: 'select', 	width,disabled:true,
				required:true, nameStr:'real_name', idStr:'salesman_id'
			},
			{ label: '发票类型',			name: 'invoice_type', 		width, required:true, type:'select', idStr:'code',disabled:true },
			{ label: '社会统一信用代码',	name: 'social_credit_code', half:true, required:true, disabled:true },
			{ label: '银行账号',			name: 'bank_account', 		half:true, required:true, disabled:true },
			{ label: '银行账号名称',		name: 'bank_account_name', 	half:true, required:true, disabled:true },
			{ label: '开户行',			name: 'opening_bank', 		half:true, required:true, disabled:true },
			{ label: '开户行地址',		name: 'opening_address', 	half:true, required:true, disabled:true },
			{ label: '开票单位编码',		name: 'hosp_code', 			half:true, required:true, disabled:true },
			{ label: '开票日期',			name: 'invoice_date', 		half:true, type:'date-time',format:1, disabled:true },
			{ type:'cpt', full:true,  component:()=>{
				return(
					<div className='fv w' style={{height:250}}>
						{ Table.call(this,{type:-1, isIndex:1, hasCurrent: false}) }
					</div>
				)
			} },
			// { type:'cpt', full:true, hide:true, component:(aa)=>{
			// 	return(
			// 		<button className='ml10 r5px cf bcm plr10 lh30 tc mb10' style={{width:60}} onClick={()=>{
			// 			let { data } = this.state
			// 			data.forEach((v,i) => v.index = i)
			// 			data.push({summary_sn:'',bill_price:'', index: data.length })
			// 			this.setState({data:[...data]})
			// 		}}>添加</button>
			// 	)
			// } },
			{ label: '备注',			name: 'remark', type: 'textarea',	full:true, disabled:true },
		]
	}
	
	model = {}
	
	uuid = null
	
	open = ({uuid,summary_sn,hosp_id}) => {
		this.uuid = uuid
		this.formRef.setValue({uuid,summary_sn,hosp_id})
		const { formData } = this.state
		$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{summary_sn,hosp_id, type:1}}).then(data=>{
			const salemanData = data.salesman
			$fn.setRow(formData, 'salesman_id', salemanData )
			this.setState({ formData })
		})
		$http.submit(this,'fi-invoice-book/info',{param:{uuid,summary_sn,hosp_id}}).then(data=>{
			const { invoice_info = []} = data
			if( $fn.hasArray(invoice_info)){
				this.setState({data: invoice_info.map((v,i)=>({ ...v, index: i})) })
			}
			this.formRef.setValue(data)
		})
		this.modal.open({ formRef:this.formRef })
		this.getSelect()
	}

	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 账期
			$fn.setRow(formData, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 发票类型
			$fn.setRow(formData, 'invoice_type', await f.getDicCache({ code:88000 } ) )
			// 医院名称
			$fn.setRow(formData, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ formData })
		})
	}
	
	// 表单
	cols = [
		{ title: '发票代号', 	dataIndex: 'invoice_no', 			...$fn.table.num, 
			render:(text,row)=>{
				return !this.uuid ? <Input
					value = {text}
					onChange = { value=>{
						row['invoice_no'] = value
					} }
				/> : text
			}
		},
		{ title: '发票编号', 	dataIndex: 'invoice_num', 	...$fn.table.num, 
			render:(text,row)=>{
				return !this.uuid ? <Input
					value = {text}
					onChange = { value=>{
						row['invoice_num'] = value
					} }
				/> : text
			}
		},
		{ title: '发票金额', 	dataIndex: 'price', 				...$fn.table.num,
			render:(text,row)=>{
				return !this.uuid ? <Input
					value = {text}
					onChange = { value=>{
						row['price'] = value
						
						this.fromData = this.state.data.map( v => ({
							price:v.price, 
							invoice_num:v.invoice_num,
							invoice_no:v.invoice_no,
						}))
					} }
					
				/> : text
			}
		 },
		// TableControls.controls.call(this, { width: 80 })
	]
	
	// 表格操作按钮
	// table_controls = (text, {index}) => {
	// 	return [
	// 		TableControls.del.call(this, { refresh:false,  onOk: (close) => {
	// 			let { data } = this.state
	// 			data.splice(index, 1)
	// 			data.forEach((v,i) => {
	// 				v.index = i
	// 			})
	// 			this.setState({data:[...data]})
	// 			close()
	// 		} })
	// 	]
	// }
	
	getBill = param => {
		const { formData } = this.state
		$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{...param, type:2}}).then(data=>{
			const salemanData = data.salesman
			$fn.setRow(formData, 'salesman_id', salemanData )
			this.formRef.setValue({
				organization_name:data.hosp.hosp_name,
				// not_invoice_price:data.bill_info.not_invoice_price,
				bill_price:data.bill_info.bill_price,
				social_credit_code:data.hosp.social_credit_code,
				bank_account:data.hosp.bank_account,
				bank_account_name:data.hosp.bank_account_name,
				wait_invoice_price:data.bill_info.wait_invoice_price,
				hosp_code:data.hosp.hosp_code,
				opening_bank:data.hosp.opening_bank
			})
			this.setState({ formData })
		})
	}
	
	
	
	render(){
		return FormModal.call(this,{ title:'查看发票申请',labelWidth:'s11', width:'50%', noOk:true, noCancel: true,
			close:()=>{
				this.setState({data:[]})
			}
		 })
	}
}

export default Index